﻿using System.Runtime.Serialization;
using Foundation;
using Protocol;
using Protocol.Authorization;
using Protocol.Transport;

namespace Telegraph.ViewModels
{
    [DataContract]
    public class KeyStoreViewModel : ViewModel
    {
        [DataMember]
        public byte[] Key { get; set; }

        [DataMember]
        public byte[] Sault { get; set; }

        [DataMember]
        public uint ServerTime { get; set; }

        public bool Authorize(DataChannel channel)
        {
            //if (Key == null)
            {
                var authorizer = new Authorizer(channel);
                var result = authorizer.Authorize();
                if (!result) return false;

                Key = authorizer.AuthKey;
                Sault = authorizer.Salt;
                ServerTime = authorizer.ServerTime;
            }

            channel.AuthorizationKey = Key;
            channel.Salt = Sault;
            channel.ServerTime = ServerTime;
            return true;
        }
    }
}
